'use strict';

/* Directives */

var directives = angular.module('myApp.directives', []);

// simple fade in animation for a touch of class...
directives.directive('fadeIn', function () {
    return {
        compile:function (elm) {
            $(elm).css('opacity', 0.0);
            return function (scope, elm, attrs) {
                $(elm).animate({ opacity:1.0 }, 1500);
            };
        }
    };
});

// this is the angular way to stop even propagation 
directives.directive('stopEvent', function () {
    return {
        restrict:'A',
        link:function (scope, element, attr) {
            element.bind(attr.stopEvent, function (e) {
                e.stopPropagation();
            });
        }
    }
});

angular.module('ng').directive('ngTarget', function($parse, $timeout) {
         var NON_ASSIGNABLE_MODEL_EXPRESSION = 'Non-assignable model expression: ';
         return {
                 restrict: "A",
                 link: function(scope, element, attr) {
                     var buildGetterSetter = function(name) {
                             var me = {};
                             me.get = $parse(name);
                             me.set = me.get.assign;
                             if (!me.set) {
                                     throw Error(NON_ASSIGNABLE_MODEL_EXPRESSION + name);
                                 }
                             return me;
                         };

                     // *********** focus ***********
                     var focusTriggerName = attr.ngTarget+"._focusTrigger";
                     var focusTrigger = buildGetterSetter(focusTriggerName);
                     var focus = buildGetterSetter(attr.ngTarget+".focus");

                     focusTrigger.set(scope, 0);
                     focus.set(scope, function() {
                             focusTrigger.set(scope, 1);
                         });

                     // $watch the trigger variable for a transition
                     scope.$watch(focusTriggerName, function(newValue, oldValue) {
                             if ( newValue > 0 ) {
                                    $timeout(function() { // a timing workaround hack
                                             element[0].focus(); // without jQuery, need [0]
                                             focusTrigger.set(scope, 0);
                                         }, 50);
                                 }
                         });

                     // *********** blur ***********
                     var blurTriggerName = attr.ngTarget+"._blurTrigger";
                     var blurTrigger = buildGetterSetter(blurTriggerName);
                     var blur = buildGetterSetter(attr.ngTarget+".blur");

                     blurTrigger.set(scope, 0);
                     blur.set(scope, function() {
                             blurTrigger.set(scope, 1);
                         });

                     // $watch the trigger variable for a transition
                     scope.$watch(blurTriggerName, function(newValue, oldValue) {
                             if ( newValue > 0 ) {
                                     $timeout(function() { // a timing workaround hack
                                             element[0].blur(); // without jQuery, need [0]
                                             blurTrigger.set(scope, 0);
                                         }, 50);
                                 }
                         });

                    // *********** select ***********
                     var selectTriggerName = attr.ngTarget+"._selectTrigger";
                     var selectTrigger = buildGetterSetter(selectTriggerName);
                     var select = buildGetterSetter(attr.ngTarget+".select");

                     selectTrigger.set(scope, 0);
                     select.set(scope, function() {
                             selectTrigger.set(scope, 1);
                         });

                     // $watch the trigger variable for a transition
                     scope.$watch(selectTriggerName, function(newValue, oldValue) {
                             if ( newValue > 0 ) {
                                     $timeout(function() { // a timing workaround hack
                                             element[0].select(); // without jQuery, need [0]
                                             selectTrigger.set(scope, 0);
                                         }, 50);
                                 }
                         });

                 }
         };
 });

directives.directive('priceFormat', ['$filter', function ($filter) {
    return {
        require: '?ngModel',
        link: function (scope, elem, attrs, ctrl) {
            if (!ctrl) return;


            ctrl.$formatters.unshift(function (a) {
                return $filter(attrs.format)(ctrl.$modelValue)
            });


            ctrl.$parsers.unshift(function (viewValue) {

                elem.priceFormat({
                    prefix: '',
                    centsSeparator: ',',
                    thousandsSeparator: '.'
                });

                return elem[0].value;
            });
        }
    };
}]);
